/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.service;

import com.aaron.platform.dao.IPayInfoDao;
import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.GczxPaymentInfo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

/**
 *
 * @author Administrator
 */
public class PaymentMgrImp implements IPaymentMgr {

    private IPayInfoDao payInfoDao;

    public IPayInfoDao getPayInfoDao() {
        return payInfoDao;
    }

    public void setPayInfoDao(IPayInfoDao payInfoDao) {
        this.payInfoDao = payInfoDao;
    }

    public GczxPaymentInfo findById(Integer orderId) {
        return payInfoDao.findById(orderId);
    }

    public GczxPaymentInfo findByInvNo(String invoiceNo) {
        List<GczxPaymentInfo> gczxPaymentInfos;
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(GczxPaymentInfo.class);
        detachedCriteria = detachedCriteria.add(Expression.eq(GczxPaymentInfo.INVOICE_NO, invoiceNo));
        gczxPaymentInfos = payInfoDao.findByCriteria(detachedCriteria);
        if (gczxPaymentInfos != null) {
            return gczxPaymentInfos.get(0);
        }
        else {
            return null;
        }
    }

    public PageInfo findByCondition(Properties condition, int startIndex, int pageSize) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(GczxPaymentInfo.class);
        if (condition != null && !condition.isEmpty()) {
            try {
                // pay pro
                int payPro = condition.getProperty(GczxPaymentInfo.PAY_PRO) == null ? 0 : Integer.valueOf(condition.getProperty(GczxPaymentInfo.PAY_PRO));
                if (payPro != 0) {
                    detachedCriteria = detachedCriteria.add(Expression.eq(GczxPaymentInfo.PAY_PRO, payPro));
                }
                double invoiceValue = condition.getProperty(GczxPaymentInfo.INVOICE_VALUE) == null ? 0 : Double.valueOf(condition.getProperty(GczxPaymentInfo.INVOICE_VALUE));
                if (invoiceValue != 0) {
                    detachedCriteria = detachedCriteria.add(Expression.eq(GczxPaymentInfo.INVOICE_VALUE, invoiceValue));
                }
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Date payDate = condition.getProperty(GczxPaymentInfo.PAY_DATE) == null ? null : df.parse(condition.getProperty(GczxPaymentInfo.PAY_DATE));
                if (payDate != null) {
                    detachedCriteria = detachedCriteria.add(Expression.eq(GczxPaymentInfo.PAY_DATE, payDate));
                }
                int enterprsieId = condition.getProperty(GczxPaymentInfo.ENTERPRISE_ID) == null ? 0 : Integer.valueOf(condition.getProperty(GczxPaymentInfo.ENTERPRISE_ID));
                if (enterprsieId != 0) {
                    detachedCriteria = detachedCriteria.add(Expression.eq(GczxPaymentInfo.ENTERPRISE_ID, enterprsieId));
                }
                String contactCode = condition.getProperty(GczxPaymentInfo.CONTACT_CODE);
                if (StringUtils.isNotEmpty(contactCode) && StringUtils.isNotBlank(contactCode)) {
                    detachedCriteria = detachedCriteria.createAlias("payDetails", "payDetial").
                            createAlias("payDetial.orderInfo", "order").add(Expression.ilike("order.contactCode", contactCode, MatchMode.ANYWHERE));
                }
                String invoiceNo = condition.getProperty(GczxPaymentInfo.INVOICE_NO);
                if (StringUtils.isNotEmpty(invoiceNo) && StringUtils.isNotBlank(invoiceNo)) {
                    detachedCriteria = detachedCriteria.add(Expression.ilike(GczxPaymentInfo.INVOICE_NO, invoiceNo, MatchMode.ANYWHERE));
                }
                String payRemarks = condition.getProperty(GczxPaymentInfo.PAY_REMARK);
                if (StringUtils.isNotEmpty(payRemarks) && StringUtils.isNotBlank(payRemarks)) {
                    detachedCriteria = detachedCriteria.add(Expression.ilike(GczxPaymentInfo.PAY_REMARK, payRemarks, MatchMode.ANYWHERE));
                }
            } catch (ParseException ex) {
                Logger.getLogger(PaymentMgrImp.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        // 排序
        detachedCriteria.addOrder(Order.desc(GczxPaymentInfo.PAY_DATE));

        return payInfoDao.findPageByCriteria(detachedCriteria,  startIndex, pageSize);
    }

    public void addPayInfo(GczxPaymentInfo gczxOrderInfo) {
        payInfoDao.save(gczxOrderInfo);
    }

    public void addOrUpdatePayInfo(GczxPaymentInfo gczxOrderInfo) {
        payInfoDao.saveOrUpdate(gczxOrderInfo);
    }

    public void updatePayInfo(GczxPaymentInfo gczxOrderInfo) {
        payInfoDao.update(gczxOrderInfo);
    }

    public void removePayInfo(Integer payId) {
        payInfoDao.removeById(payId);
    }

    public void removePayInfo(GczxPaymentInfo gczxPaymentInfo) {
        payInfoDao.remove(gczxPaymentInfo);
    }
}
